package com.cainaer.product.controller;


import com.cainaer.common.core.utils.StringUtils;
import com.cainaer.common.core.web.controller.BaseController;
import com.cainaer.common.core.web.domain.AjaxResult;
import com.cainaer.common.redis.service.RedisService;
import com.cainaer.product.domain.ProductSku;
import com.cainaer.product.domain.SystemAttribute;
import com.cainaer.product.domain.vo.SkuVo;
import com.cainaer.product.service.IProductSkuService;
import com.cainaer.product.service.ISystemAttributeService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 系统规格表 前端控制器
 * </p>
 *
 * @author Tina
 * @since 2020-09-11
 */
@RestController
@RequestMapping("/systemAttribute/")
public class SystemAttributeController extends BaseController {

    @Autowired
    private ISystemAttributeService systemAttributeService;

    @Autowired
    private IProductSkuService productSkuService;

    @Autowired
    private RedisService redisService;

    private Logger logger = LoggerFactory.getLogger(SystemAttributeController.class);

    /**
     * 推荐系统规格
     *
     * @param systemAttribute
     * @return
     */
    @GetMapping("list")
    public AjaxResult selectSystemAttribute(SystemAttribute systemAttribute) {

        List<SystemAttribute> systemAttributes = systemAttributeService.selectSystemAttribute(systemAttribute);
        List<SystemAttribute> respList = new ArrayList<SystemAttribute>();
        for (SystemAttribute attribute : systemAttributes) {
            respList.add(attribute);
        }
        List<ProductSku> productSkus = productSkuService.selectProductSkuByAttributeId(null);
        for (SystemAttribute attribute : respList) {
            attribute.setProductSkus(getChild(attribute.getSystemAttributeId(), productSkus));
        }
        return AjaxResult.success(respList);
    }

    /**
     * 递归查找sku
     * id 当前规格id
     */
    public List<ProductSku> getChild(Long systemAttributeId, List<ProductSku> productSkus) {
        List<ProductSku> childList = new ArrayList<>();
        for (ProductSku productSku : productSkus) {
            if (productSku.getSystemAttributeId() == systemAttributeId) {
                childList.add(productSku);
            }
        }
        return childList;
    }

}
